#!/bin/bash
if [ -e /tmp/proxy ];then
    source /tmp/proxy
fi
if ! command -v pacman >/dev/null 2>&1;then
    echo "Error: Not archlinux!"
    exit 1
fi
if [[ $EUID != 0 ]];then
    echo "Error:need root priviledge!"
    exit 1
fi

user=${SUDO_USER:-$(whoami)}
home=$(eval echo ~$user)

basicApps="tmux neovim vim git openssh net-tools dnsutils tree screenfetch wget cronie unzip zsh ctags fontconfig pv lsof"
devApps="base-devel linux-headers clang cmake python boost python-pip go go-tools"

read -p "Install development apps (ie:clang python boost go) [Y/n] "  installDev
echo "pacman -Syu ..."
pacman -Syu --noconfirm --needed >/dev/null 2>&1 || { echo "pacman upate failed!";exit 1; }
for app in ${basicApps};do
    echo "Install $app..."
    pacman -S $app --noconfirm --needed >/dev/null 2>&1
done

if [[ "$installDev" != [nN] ]];then
    for app in ${devApps};do
        echo "Install $app..."
        pacman -S $app --noconfirm --needed >/dev/null 2>&1
    done
fi

if dmesg | grep -iq hypervisor;then
    if dmesg | grep -iq vbox;then
        echo "Running on VirtualBox,installing virtualbox-guest-utils..."
        pacman -S virtualbox-guest-utils --noconfirm --needed
    fi
fi

systemctl enable sshd
systemctl enable dhcpcd
systemctl enable cronie

read -p "Install Desktop Environment? [y/N] "  de
if [[ "$de" == [yY] ]];then
    while read -p "Install which display manager ?[lxdm or sddm ] (prefer sddm) " dm;do
        if echo $dm | grep -P '^(lxdm|sddm)$';then
            echo "$dm will be installed"
            break
        else
            echo "Input error, try again!"
        fi
    done

    #the basis of all DE
    pacman -S xorg --noconfirm --needed
    pacman -S xorg-xinit --noconfirm --needed
    #install dm
    pacman -S "$dm" --noconfirm --needed
    systemctl enable "$dm"

    # pacman -S adobe-source-han-sans-cn-fonts --noconfirm  --needed
    # pacman -S wqy-zenhei --noconfirm  --needed
    pacman -S wqy-microhei --noconfirm  --needed #浏览器中文乱码
    pacman -S bdf-unifont  --noconfirm  --needed #vim listchars character font
    pacman -S gnome-terminal --noconfirm  --needed
    pacman -S gnome-disk-utility --noconfirm --needed

    read -p "Install lxqt ? (Y/n)" lxqt
    if [[ "$lxqt" != [nN] ]];then
        pacman -S lxqt oxygen-icons --noconfirm --needed
        pacman -S libstatgrab --noconfirm --needed
        pacman -S qterminal --noconfirm --needed
        sddm --example-config > /etc/sddm.conf

    fi

    read -p "Install lxde ?(y/N) " lxde
    if [[ "$lxde" =~ [yY] ]];then
        #安装lxde桌面环境相关
        pacman -S lxde-common --noconfirm --needed
        pacman -S lxsession --noconfirm --needed
        pacman -S lxpanel --noconfirm --needed
        pacman -S openbox --noconfirm --needed
        pacman -S pcmanfm --noconfirm --needed
        # sed -i 's/^# \(session.*startlxde$\)/\1/' /etc/lxdm/lxdm.conf
    fi

    read -p "Install xfce ?(y/N) " xfce
    if [[ "$xfce" =~ [yY] ]];then
        pacman -S xfce4 xfce4-goodies --noconfirm --needed
    fi

    read -p "Install Cinnamon ?(y/N) " cinnamon
    if [[ "$cinnamon" =~ [yY] ]];then
        #安装cinnamon桌面环境
        pacman -S cinnamon --noconfirm --needed
        pacman -S gnome-terminal --noconfirm --needed
    fi

    read -p "Install KDE ? (y/N) " kde
    if [[ "$kde" =~ [yY] ]];then
        pacman -S plasma kde-applications --noconfirm --needed
        echo "exec startkde" >> /etc/X11/xinit/xinitrc

        echo 'export GTK_IM_MODULE=fcitx' >> ${home}/.xprofile
        echo 'export QT_IM_MODULE=fcitx' >> ${home}/.xprofile
        echo 'export XMODIFIERS="@im=fcitx"' >> ${home}/.xprofile
    fi
    read -p "Install fcitx ?(Y/n) " fcitx
    if [[ "$fcitx" != [nN] ]];then
        pacman -S fcitx-im --noconfirm --needed
        pacman -S fcitx-configtool --noconfirm --needed
        pacman -S fcitx-sunpinyin --noconfirm --needed

        echo 'export GTK_IM_MODULE=fcitx' >> ${home}/.xprofile
        echo 'export QT_IM_MODULE=fcitx' >> ${home}/.xprofile
        echo 'export XMODIFIERS="@im=fcitx"' >> ${home}/.xprofile
    fi

fi
